﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace Graphic_test
{
    public partial class Form1 : Form
    {
        Bitmap bitmap;
        Graphics graphic1,graphic2;
        int intlen = 0;
        string sr;
        static bool last_obs = false;
        public Form1()
        {
            InitializeComponent();
        }
        void refe()
        {
            pictureBox1.Image = bitmap;
        }
        void MyRobot()
        {
            graphic1.FillEllipse(new SolidBrush(Color.Blue), 240, 480, 120, 120);
            Point point1 = new Point(300, 480);
            Point point2 = new Point(352, 570);
            Point point3 = new Point(248, 570);
            Point[] curvePoints = { point1, point2, point3 };
            graphic1.FillPolygon(new SolidBrush(Color.Yellow), curvePoints);
            refe();
        }
        void obtacle(int px,int py,int pl)
        {
            graphic2.DrawRectangle(new Pen(Color.White), px - 10, py - 10, pl, 20);
            refe();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            graphic1 = Graphics.FromImage(bitmap);
            graphic2 = Graphics.FromImage(bitmap);
            MyRobot();
        }
        
        private void Btext_Click(object sender, EventArgs e)
        {
            obtacle(30, 30,40);
        }
        
        private void On_Com(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string s; 
            char[] c = new char[5];
            s = Com.ReadExisting();
            sr = s;
            if(last_obs==true)
            {
                graphic2.Clear(Color.Black);
                MyRobot();
                Draw(sr);
                last_obs = false;
            }
            else
            {
                Draw(sr);
            }
        }
        void Draw(string c)
        {
            int px1, py1,t_px1,t_py1;
            int px2, py2, t_px2, t_py2;
            int px3, py3, t_px3, t_py3;
            int px4, py4, t_px4, t_py4;
            px2 = (int)(c[1] * (Math.Cos(c[2] * (Math.PI) / 180)));
            py2 = (int)(c[1] * (Math.Sin(c[2] * (Math.PI) / 180)));
            t_px2 = 300 + px2;
            t_py2 = 540 - py2;
            px3 = (int)(c[3] * (Math.Cos(c[4] * (Math.PI) / 180)));
            py3 = (int)(c[3] * (Math.Sin(c[4] * (Math.PI) / 180)));
            t_px3 = 300 + px3;
            t_py3 = 540 - py3;
            Point p2 = new Point(t_px2, t_py2);
            Point p3 = new Point(t_px3, t_py3);
            px1 = (int)((c[1]+20) * (Math.Cos(c[2] * (Math.PI) / 180)));
            py1 = (int)((c[1]+20) * (Math.Sin(c[2] * (Math.PI) / 180)));
            t_px1 = 300 + px1;
            t_py1 = 540 - py1;
            px4 = (int)((c[3]+20) * (Math.Cos(c[4] * (Math.PI) / 180)));
            py4 = (int)((c[3]+20) * (Math.Sin(c[4] * (Math.PI) / 180)));
            t_px4 = 300 + px4;
            t_py4 = 540 - py4;
            Point p1 = new Point(t_px1, t_py1);
            Point p4 = new Point(t_px4, t_py4);
            Point[] obs = { p1, p2, p3, p4 };
            graphic2.DrawPolygon(new Pen(Color.White,3.0f),obs);
            refe();
        }

        private void BConnect_Click(object sender, EventArgs e)
        {
            if (LbStatus.Text == "DISCONNECT")
            {
                Com.PortName = cbSecCom.Text;
                Com.Open();
                LbStatus.Text = "CONNECT";
                BConnect.Text = "DISCONNECT";
                pStatus.Value = 100;
            }
            else
            {
                Com.Close();
                LbStatus.Text = "DISCONNECT";
                BConnect.Text = "CONNECT";
                pStatus.Value = 0;
            }
        }
        private delegate void DlDisplay(string s);
        private void Display(string s)
        {
            if (txtReceive.InvokeRequired)
            {
                DlDisplay sd = new DlDisplay(Display);
                txtReceive.Invoke(sd, new object[] { s });
            }
            else
            {
                txtReceive.Text = s;
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            if (intlen != ports.Length)
            {
                intlen = ports.Length;
                cbSecCom.Items.Clear();
                for (int j = 0; j < intlen; j++)
                {
                    cbSecCom.Items.Add(ports[j]);
                }

            }
        }

       

       
    }
}
